package com.hanock.sm.sound;

import java.util.HashMap;

import com.hanock.sm.app.GlobalState;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.SystemClock;
import android.util.Log;

public class SoundManager {
	
	private static final int SOUND_INTERVAL_MIN = 35;//70

	private SoundPool soundPool;
	private HashMap<SoundType, Integer> soundMap;
	private HashMap<SoundType, MediaPlayer> backgroundMap;
	private HashMap<SoundType, MediaPlayer> loopMap;
	
	private HashMap<SoundType, Long> soundTimeMap;
	
	private MediaPlayer currentBackground;
	
	private boolean released;
	private boolean loaded;
	
	private static final int MAX_STREAM = 15;//5
	

	public SoundManager(Context context) {
	}
	
	public void load(Context context){
		
		if (loaded)
			return;
		
		soundPool = new SoundPool(MAX_STREAM, AudioManager.STREAM_MUSIC, 0);
		
		soundMap = new HashMap<SoundType, Integer>();
		backgroundMap = new HashMap<SoundType, MediaPlayer>();
		soundTimeMap = new HashMap<SoundType, Long>();
		loopMap = new HashMap<SoundType, MediaPlayer>();
		

		//
		/*MediaPlayer mainPlayer = MediaPlayer.create(context, R.raw.background1);
		mainPlayer.setLooping(true);
		mainPlayer.setVolume(SoundType.BACKGROUND1.getVolume(), SoundType.BACKGROUND1.getVolume());
		backgroundMap.put(SoundType.BACKGROUND1, mainPlayer);
		
		
		soundMap.put(SoundType.EAT_APPLE, soundPool.load(context, R.raw.apple3, 1));
		soundMap.put(SoundType.JUMP, soundPool.load(context, R.raw.jump, 1));
		soundMap.put(SoundType.MUSHROOM_BOUNCE, soundPool.load(context, R.raw.mushroom, 1));
		*/
		loaded = true;
	}
	
	public void switchBackground(SoundType type){
		
		if (released || !loaded)
			return;
		
		MediaPlayer mediaPlayer = backgroundMap.get(type);
		
		if (mediaPlayer == null)
			return;
		
		if (mediaPlayer == currentBackground){
			playBackground();
			return;
		}
		
		for (MediaPlayer mp: backgroundMap.values()){
			if (mp.isPlaying() && mp != mediaPlayer)
				mp.pause();
		}
		
		mediaPlayer.seekTo(0);
		currentBackground = mediaPlayer;
		
		playBackground();
	}
	
	public void playBackground(){
		
		if (released || !loaded)
			return;
		
		if (currentBackground == null)
			return;
		if (! currentBackground.isPlaying()){
			currentBackground.start();
		}
	}
	
	public void restartBackground(){
		
		if (released || !loaded)
			return;
		
		if (currentBackground == null)
			return;
		if (currentBackground.isPlaying()){
			currentBackground.seekTo(0);
		}
		else{
			currentBackground.start();
		}
	}
	
	public void pauseBackground(){
		
		if (released || !loaded)
			return;
		
		if (currentBackground == null)
			return;
		
		if (currentBackground.isPlaying()){
			currentBackground.pause();
		}
	}
	
	public void playSound(SoundType type){
		
		if (released || !loaded)
			return;
		
		if (soundMap.containsKey(type) && !GlobalState.background){
			
			long cur = SystemClock.elapsedRealtime();
			Long pre = soundTimeMap.get(type);
			
			if (pre == null){
				soundPool.play(soundMap.get(type), type.getVolume(), type.getVolume(), 0, 0, 1);
				soundTimeMap.put(type, cur);
			}
			else{

				int interval = SOUND_INTERVAL_MIN;
				
				if (cur > pre + interval){
					soundPool.play(soundMap.get(type), type.getVolume(), type.getVolume(), 0, 0, 1);
					soundTimeMap.put(type, cur);
				}
			}
		}
	}
	
	public void setLoopSoundVolume(SoundType type, float volume){
		
		if (released || !loaded)
			return;
		
		if (loopMap.containsKey(type) && !GlobalState.background){
			
			MediaPlayer mp = loopMap.get(type);
			mp.setVolume(volume, volume);
		}
	}
	
	public void playLoopSound(SoundType type){
		
		if (released || !loaded)
			return;
		
		if (loopMap.containsKey(type) && !GlobalState.background){
			
			MediaPlayer mp = loopMap.get(type);
			if (!mp.isPlaying()){
				mp.start();
			}
		}
	}
	
	public void pauseLoopSound(){
		 
		if (released || !loaded)
			return;
		
		for (MediaPlayer mp : loopMap.values()){
			if (mp.isPlaying()){
				mp.pause();
			}
		}
	}
	
	public void pauseLoopSound(SoundType type){
		
		if (released || !loaded)
			return;
		
		if (loopMap.containsKey(type)){
			
			MediaPlayer mp = loopMap.get(type);
			if (mp.isPlaying()){
				mp.pause();
			}
		}
	}

	public void seekToBeginLoopSound(SoundType type){
		
		if (released || !loaded)
			return;
		
		if (loopMap.containsKey(type) && !GlobalState.background){
			
			MediaPlayer mp = loopMap.get(type);
			mp.seekTo(0);
		}
	}
	
	public void release(){
		if (!loaded)
			return;
		
		if (soundPool != null){
			soundPool.release();
			soundPool = null;
		}
		
		if (backgroundMap != null){
			for (MediaPlayer mp: backgroundMap.values())
				mp.release();
			backgroundMap = null;
		}
		
		if (loopMap != null){
			for (MediaPlayer mp: loopMap.values())
				mp.release();
			loopMap = null;
		}
		
		released = true;
	}
}








